package cn.itsource.service.impl;

import cn.itsource.api.LoginRemoteApi;
import cn.itsource.constants.RConst;
import cn.itsource.pojo.UserLevelMenu;
import cn.itsource.pojo.domain.Customer;
import cn.itsource.mapper.CustomerMapper;
import cn.itsource.pojo.domain.Login;
import cn.itsource.pojo.dto.CustomerRegisterDto;
import cn.itsource.result.JSONResult;
import cn.itsource.service.ICustomerService;
import cn.itsource.service.WechatTemplate;
import cn.itsource.utils.BitStatesUtils;
import cn.itsource.utils.IdUtil;
import cn.itsource.utils.NameUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author zong
 * @since 2024-09-06
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Autowired
    private WechatTemplate wechatTemplate;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private LoginRemoteApi loginRemoteApi;

    /**
     * 代码里面不能出现 魔法值 = 未定义就开始用的值
     * @param dto
     * @return
     */
    @Override
    public JSONResult customerRegister(CustomerRegisterDto dto) {
        String openId = wechatTemplate.getOpenId(dto.getCode());
        // String accessToken = wechatTemplate.getAccessToken();
        // String userPhone = wechatTemplate.getUserPhone(dto.getPhoneCode(), accessToken);
        //保存之前先查询
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<Customer>()
                .eq(Customer::getOpenId,openId)
                .select(Customer::getOpenId);
        Customer customer = customerMapper.selectOne(wrapper);
        if(customer == null){
            Customer newCustomer = new Customer();
            newCustomer.setId(IdUtil.getId());
            newCustomer.setSex(RConst.Num.INT_1);
            newCustomer.setPhone("18723000000");
            newCustomer.setCreateTime(new Date());
            newCustomer.setOpenId(openId);
            newCustomer.setBitState(BitStatesUtils.OP_DEFAULT);
            newCustomer.setName(NameUtil.getName());
            newCustomer.setLevel(UserLevelMenu.BRONZE.getKey());
            customerMapper.insert(newCustomer);
            loginRemoteApi.create(getLogin(newCustomer,openId));
        }
        return null;
    }

    /**
     * 构建一个login
     * @param customer
     * @param openId
     * @return
     */
    private static Login getLogin(Customer customer, String openId) {
        Login login = new Login();
        login.setId(customer.getId());
        login.setUsername(customer.getName());
        login.setPassword("123456");
        login.setType(RConst.Num.INT_2);
        login.setAdmin(RConst.Num.INT_0);
        login.setNickName(customer.getName());
        login.setOpenId(openId);
        login.setName(customer.getName());
        login.setCreateTime(customer.getCreateTime());
        login.setPhone(customer.getPhone());
        return login;
    }
}
